package com.dzl.admin;

import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.enterprise.pojo.WelfareEnterpriseConfig;
import com.dzl.enterprise.pojo.vo.XcxEnterpriseVo;
import com.dzl.enterprise.service.WelfareEnterpriseConfigService;
import com.dzl.enterprise.service.WelfareEnterpriseService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 获取小程序所属企业基础信息
 * @author liuguanjun
 * @date 2022-01-08 14:13:01
 * @return
 **/
@Slf4j
@RestController
@RequestMapping("/welfareBase")
public class WelfareBaseInfoController extends BaseController{

	@Autowired
	private WelfareEnterpriseService welfareEnterpriseService;

	@Autowired
	private WelfareEnterpriseConfigService enterpriseConfigService;

	/**
	 * 获取小程序所属企业基础信息
	 * @param appid  小程序id
	 * @author liuguanjun
	 * @since 2022-01-08 14:17:11
	 **/
	@GetMapping(value = "getEnterpriseInfo")
	public JsonResult<XcxEnterpriseVo> getAppletInfo(String appid) {
		if(EmptyUtils.isEmpty(appid)){
			return JsonResult.paramErrWithMsg("appid不能传空");
		}
		XcxEnterpriseVo enterpriseVo=new XcxEnterpriseVo();
		WelfareEnterprise welfareEnterpriseQuery = new WelfareEnterprise();
		if(!appid.equals("wx451f203862d10199")){//如果是福力兔
			welfareEnterpriseQuery.setStatus(CommonConstants.YES);
		}
		welfareEnterpriseQuery.setAppid(appid);
		List<WelfareEnterprise> list = welfareEnterpriseService.select(welfareEnterpriseQuery);
		if(EmptyUtils.isNotEmpty(list)){
			WelfareEnterprise welfareEnterprise = list.get(0);
			welfareEnterprise.setApiSecret(null);
			welfareEnterprise.setMchKey(null);
			welfareEnterprise.setAppsecret(null);
			//查询企业独立部署配置信息
			WelfareEnterpriseConfig enterpriseConfig=new WelfareEnterpriseConfig();
			enterpriseConfig.setEnterpriseId(welfareEnterprise.getId());
			BeanUtils.copyProperties(welfareEnterprise,enterpriseVo);
			enterpriseVo.setEnterpriseConfig(enterpriseConfigService.selectOne(enterpriseConfig));
			return JsonResult.success(enterpriseVo);
		}else{
			return JsonResult.failWithMsg("企业未注册");
		}
	}

	/**
	 * 描述: 获取数据读取 来源自行配置数据还是引用福利兔企业数据
	 * @date 2022/2/21 15:06
	 * @author wangchao
	 * @Param []
	 * @return java.lang.Integer
	**/
	public Integer getDataSourcesEnterpriseId(){
		Integer enterpriseId=CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID;//默认平台
		//判断当前企业是自行配置还是同步福利兔
		HttpServletRequest request = getHttpServletRequest();
		String enterpriseId_ = request.getHeader("enterpriseId");
		if(EmptyUtils.isNotEmpty(enterpriseId_)){
			WelfareEnterprise welfareEnterprise=welfareEnterpriseService.selectByPrimaryKey(Integer.valueOf(enterpriseId_));
			if(welfareEnterprise!=null && EmptyUtils.isNotEmpty(welfareEnterprise.getAppRuleConfig()) && welfareEnterprise.getAppRuleConfig()==CommonConstants.APP_RULE_CONFIG.SELF_CONFIG){
				enterpriseId=welfareEnterprise.getId();
			}
		}
		return enterpriseId;
	}
}
